package edu.usm.roberge.client;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;

import edu.usm.roberge.shared.WeatherForecast;

public class WeatherAsyncProvider extends AsyncDataProvider<WeatherForecast> {
	
	private final GolfCourseServiceAsync golfCourseService;
	private boolean loading;

	public WeatherAsyncProvider(GolfCourseServiceAsync golfCourseService) {
		this.golfCourseService = golfCourseService;
	}
	
	public void getWeatherForecast(String city, String state) {
		loading = true;
		golfCourseService.getWeatherForecast(city, state, new AsyncCallback<List<WeatherForecast>>() {
			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(List<WeatherForecast> result) {
				loading = false;
				updateRowData(0, result);
			}
		});
	}

	@Override
	protected void onRangeChanged(HasData<WeatherForecast> display) {
		if (!loading) {
			updateRowCount(0, true);
		}
	}

}
